<?php
session_start();
require_once "lib/standard.php";
Globals::init();

if (!isset($_SESSION['login'])){
//header("Location: http://" . $_SERVER["SERVER_NAME"] . "/panospanos2/account/login.php");
header("Location: " . Globals::$server . "account/login.php");
//die("login not set");
}
header('Content-Type: text/html; charset=UTF-8');
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ραντεβού</title>
<link href="styles.css" rel="stylesheet" type="text/css" />

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript" src="main.js"></script>
<script type="text/javascript" src="skiniko/komotis.js"></script>
<script type="text/javascript" src="skiniko/ipiresia.js"></script>
<script type="text/javascript" src="skiniko/katigoria.js"></script>
<script type="text/javascript">

var komotes = []; 
<?php
require_once "lib/load_komotes.php";
?>
var ipiresies = [];
<?php
require_once "lib/load_ipiresies.php";
?>
var katigories = [];
<?php
require_once "lib/load_katigories.php";
?>

function init() {

	var kom = komotes.length;
	var ipi = ipiresies.length;
	var kat = katigories.length;
		
	for(var x = 0; x < kom; x++) {
		if(komotes[x]) {
			komotes[x].init();
		}
	}

	for(var x = 0; x < kat; x++) {
		katigories[x].init();
	}

	for(var x = 0; x < ipi; x++) {
		ipiresies[x].init();
	}
	
	var keys_table = document.getElementById("keys_table");
	for(var x = 0; x < kat; x++) {
//alert(katigories[x].dom.tr.id);
		keys_table.appendChild(katigories[x].dom.tr);
		for(var y = 0; y < ipi; y++) {
			if(ipiresies[y].katigoria == katigories[x].id) {
				katigories[x].dom.tr.appendChild(ipiresies[y].dom.td);
			}
		}
	}
	
	var tableRows = [];
	var mainTable = document.getElementById("main_table");
	var imerominia = document.createElement("td");
	imerominia.setAttribute("colspan", kom);
	imerominia.setAttribute("class", "imerominia");
	
	var day = document.createElement("select");
	day.setAttribute("name", "day");
	var dayOptions = [];
	for(var i = 1; i <= 31; i++) {
		dayOptions[i] = document.createElement("option");
		dayOptions[i].setAttribute("value", i);
		dayOptions[i].appendChild(document.createTextNode(i));
		day.appendChild(dayOptions[i]);
	}
	imerominia.appendChild(day);
	
	var month = document.createElement("select");
	month.setAttribute("name", "month");
	var monthOptions = [];
	for(var i = 1; i <= 12; i++) {
		monthOptions[i] = document.createElement("option");
		monthOptions[i].setAttribute("value", i);
		monthOptions[i].appendChild(document.createTextNode(i));
		month.appendChild(monthOptions[i]);
	}
	imerominia.appendChild(month);
	
	var year = document.createElement("select");
	year.setAttribute("name", "year");
	var yearOptions = [];
	for(var i = 2014; i <= 2050; i++) {
		yearOptions[i] = document.createElement("option");
		yearOptions[i].setAttribute("value", i);
		yearOptions[i].appendChild(document.createTextNode(i));
		year.appendChild(yearOptions[i]);
	}
	imerominia.appendChild(year);
	
	var imerominiaRow = document.createElement("tr");
	imerominiaRow.appendChild(imerominia);
	mainTable.appendChild(imerominiaRow);
	
<?php
	$queryImerominia = "SELECT  `imerominia` 
		FROM  `ergasia` 
		WHERE  `id` 
		IN (

		SELECT MAX(  `id` ) 
		FROM  `ergasia`
		)";
	$resultImerominia = mysqli_query(Globals::$db, $queryImerominia);
	$rowImerominia = mysqli_fetch_array($resultImerominia, MYSQLI_ASSOC);
	$imerominia = explode("-", $rowImerominia['imerominia']);
	$year = $imerominia[0];
	$month = $imerominia[1];
	$day = ($imerominia[2] < 31) ? $imerominia[2] + 1 : 1;
	echo "dayOptions[" . $day . "].setAttribute('selected', 'selected'); \n";
	echo "monthOptions[" . $month . "].setAttribute('selected', 'selected'); \n";
	echo "yearOptions[" . $year . "].setAttribute('selected', 'selected'); \n";
	echo "console.log('" . $rowImerominia['imerominia'] . "'); \n";

?>
	var tr = document.createElement("tr");

	for(var k = 0; k < kom; k++) {
		var td = document.createElement("td");
		td.setAttribute("class", "komotis_tag");
		td.appendChild(komotes[k].dom.name_button);
		tr.appendChild(td);
	}
	mainTable.appendChild(tr);
	var rows = 21;
	for(var i = 1; i <= rows; i++) {
		tableRows[i] = document.createElement("tr");
		tableRows[i].setAttribute("class", "data_row");
		for(var j = 0;j < kom; j++) {
			var td = document.createElement("td");
			td.appendChild(komotes[j].dom.perigrafiInput[i]);
			td.appendChild(komotes[j].dom.hiddenInput[i]);
			td.appendChild(komotes[j].dom.posoInput[i]);
			tableRows[i].appendChild(td);
		}
	mainTable.appendChild(tableRows[i]);
	}
	
	// for(var j = 0; j < kom; j++) {
			// tableRows[rows].appendChild(komotes[j].dom.hidden);
		// }
	var hiddenkomLength = document.createElement("input");
	hiddenkomLength.setAttribute("type", "hidden");
	hiddenkomLength.setAttribute("name", "hiddenkomLength");
	hiddenkomLength.setAttribute("value", kom);
	tableRows[rows].appendChild(hiddenkomLength);
	var hiddenRowsLength = document.createElement("input");
	hiddenRowsLength.setAttribute("type", "hidden");
	hiddenRowsLength.setAttribute("name", "hiddenRowsLength");
	hiddenRowsLength.setAttribute("value", rows);
	tableRows[rows].appendChild(hiddenRowsLength);

	globals.curTarget.kom = komotes[0];
	globals.curTarget.row = globals.curTarget.kom.currentRow;
	
	var mainForm = document.getElementById("mainForm");
	mainForm.setAttribute("onSubmit", "globals.send(" + kom + ")");
}

$( document ).ready(function() {
    init();
});

</script>
</head>
<body>
<div id="keys">
<table id="keys_table" border="0" >
</table>
</div>
<p>
<?php print $_SESSION['login'];?>
</p>
<form action="egrafi.php" id="mainForm">
<table id="main_table" border="1" cellspacing="2" cellpadding="1">
</table>
<input type="submit"/>
</form>
<div id="statsDiv">
<a href="showStats.php">Στατιστικά</a>
</div>
</body>
</html>
